package org.cug.controller;

import lombok.extern.slf4j.Slf4j;

import org.cug.pojo.InternshipTeaching;
import org.cug.pojo.Result;
import org.cug.service.InternshipTeachingService;
import org.cug.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/internshipTeaching")
@Slf4j
/**
 * 该控制器用于处理实习教学相关的代码的增删改查，操纵的数据库为internship_teaching
 * state:已测验
 */
public class InternshipTeachingController {
    @Autowired
    private InternshipTeachingService internshipTeachingService;

    @GetMapping
    public Result list(){
        log.info("查询所有实习生授课信息");
        //本地线程获取
        Map<String,Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        List<InternshipTeaching> internshipTeachings =internshipTeachingService.list(userId);
        return Result.success(internshipTeachings);
    }

    @PostMapping
    public Result add(@RequestBody InternshipTeaching internshipTeaching){
        log.info("增加实习生授课信息{}", internshipTeaching);
        Map<String,Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        internshipTeaching.setUserId(userId);
        internshipTeachingService.add(internshipTeaching);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除实习生授课信息{}",ids);
        internshipTeachingService.delete(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectid(@PathVariable Integer id) {
        log.info("查询id{}",id);
        InternshipTeaching internshipTeaching =internshipTeachingService.selectid(id);
        return Result.success(internshipTeaching);
    }

    @PutMapping
    public Result alterbyid(@RequestBody InternshipTeaching internshipTeaching){
        log.info("修改实习生授课信息{}", internshipTeaching);
        internshipTeachingService.alterbyid(internshipTeaching);
        return Result.success();
    }
}
